﻿using CommonLibrary;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading;
using System.Web;

namespace MessageNotify.Common
{
    public class MessageCommon
    {
        private static DbHelper dbHelper;
        private static string connStr = "Data Source=zyq;User ID=sa;Password=sa;Initial Catalog=MessageTest;Persist Security Info=True;Application Name=CMSQBS";
        public static void Start()
        {
            ThreadStart threadLoop = () =>
            {
                dbHelper = new DbHelper(connStr);
                dbHelper.TimeOut = 20;
                
                while (true)
                {
                    try
                    {
                        dbHelper.AddParameter("@ReturnMessage", "", System.Data.ParameterDirection.Output);
                        dbHelper.ExecuteProcedure<DataTable>("P_WaitMessage");

                        string msg = dbHelper.GetParameterValue("@ReturnMessage").ToString();
                        dbHelper.ClearParatmeter();
                        if (!string.IsNullOrEmpty(msg))
                        {
                            //do something
                            LogHelper.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " ：" + msg);
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ex);
                    }
                    
                }
            };
               var thread = new Thread(threadLoop)
            {
                IsBackground = true
            };
            thread.Start();
        }
    }
}